package com.seed.core.pojo;

import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Getter;

import java.io.Serial;
import java.io.Serializable;

@Getter
@Schema(description = "结果")
public class R<T> implements Serializable {

    @Serial
    private static final long serialVersionUID = 1L;

    /**
     * 状态码
     */
    @Schema(description = "状态码")
    private final int code;

    /**
     * 返回消息
     */
    @Schema(description = "消息")
    private final String msg;

    /**
     * 返回数据
     */
    @Schema(description = "结果响应")
    private final T data;

    private R(int code, String msg, T data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    public static <T> R<T> result(int code, String msg, T data) {
        return new R<>(code, msg, data);
    }

    public static <T> R<T> result(int code, String msg) {
        return new R<>(code, msg, null);
    }

    public static <T> R<T> success(String msg, T data) {
        return result(200, msg, data);
    }

    public static <T> R<T> success(T data) {
        return result(200, "操作成功", data);
    }

    public static <T> R<T> ok(T data) {
        return success(data);
    }
    public static <T> R<T> data(T data) {
        return success(data);
    }
    public static <T> R<T> success(String msg) {
        return result(200, msg);
    }
    public static <T> R<T> ok(String msg) {
        return result(200, msg);
    }

    public static <T> R<T> error(String msg, T data) {
        return result(500, msg, data);
    }

    public static <T> R<T> error(String msg) {
        return result(500, msg);
    }
}
